package cn.t17.appsys.exception;

import cn.t17.appsys.dto.CommonResponse;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * GlobalExceptionAdvice class
 *
 * @author Administrator
 * @date 2021/9/16
 */
@ControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(ServletRequestBindingException.class)
    @ResponseBody
    public CommonResponse<?> handleBusinessException(ServletRequestBindingException ex){
        CommonResponse<?> response = new CommonResponse<>();
        response.setCode(-1);
        response.setData(null);
        response.setMessage(ex.getMessage());
        return  response;
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public CommonResponse<?> handleBusinessException(BusinessException ex){
        CommonResponse<?> response = new CommonResponse<>();
        response.setCode(-1);
        response.setData(null);
        response.setMessage(ex.getMessage());
        return  response;
    }
}
